<?php
namespace app\common\model;

use think\Model;
use think\facade\Db;

/**
 * 模型基类
 * 使用模型关联替代关联查询
 */
class ModelBase extends Model
{
    //数据集
    public $resultSetType = 'collection';

    //插入单条数据
    final public function addInfo($data = []){
        return $this->save($data);
    }

    //插入多条数据
    final public function addList($data){
        $result = $this->insertAll($data);
        return $result;
    }

    //删除数据
    final public function deleteData($where){
        $result = $this->where($where)->delete();
        return $result;
    }

    //获取条件查询单条数据
    final public function getInfo($where = [], $field = true){
        $info = $this->field($field)->where($where)->find();
        $info = !empty($info) ? $info->toArray() : [];
        return $info;
    }
    //获取条件查询单条数据(需要联合查询的)
    final public function getJoinInfo($where = [], $field = true){
        $info = $this->field($field)->where($where)->find();
        return $info;
    }

    //获取条件查询数据
    final public function getList($where = [], $field = true, $order = 'id asc', $offset = 0,$length = 15){
        $list = $this->field($field)->where($where)->order($order)->limit($offset,$length)->select();
        $list = !empty($list) ? $list->toArray() : [];
        return $list;
    }

    //获取条件查询数据(需要联合查询的)
    final public function getJoinList($where = [], $field = true, $order = 'id asc', $offset = 0,$length = 15){
        $list = $this->field($field)->where($where)->order($order)->limit($offset,$length)->select();
        return $list;
    }

    //获取所有数据条数
    final public function getAllCount($where){
        $count = $this->where($where)->count();
        return $count;
    }

    //根据条件修改单条数据
    final public function updateInfo($where = [], $data = []){
        return $this->where($where)->save($data);
    }

    //根据条件修改修改多条数据
    final public function updateList($data = []){
        return $this->saveAll($data);
    }

    //原生查询
    final public function query($sql){
        return Db::query($sql);
    }

    //原生执行
    final public function execute($sql){
        return Db::execute($sql);
    }
}
